empty = function (text) {
  return text == null || text == "" || text == "" || text == 0;
};
price = !empty(localStorage.getItem("price"))
? localStorage.getItem("price").split("\uE333")
: [{ name: "", price: 0 }];
try {
for (var i = 0; i < price.length; i++) {
  //console.log(price[i])
  price[i] = JSON.parse(price[i]);
}
} catch {
alert("设置文件损坏，已重置设置。");
price = [{ name: "", price: 0 }];
//console.log("error: file=")
//console.log(localStorage.getItem("price"))
}

function update() {
  content = document.getElementById("content");
  topnav = document.getElementById("topnav");
  if (window.location.hash != "#save") {
    content.innerHTML = "";
    if (window.location.hash == "") {
      topnav.innerHTML =
        '<li><span>管理</span></li><li><a href="index.html">返回</a></li><li><a href="#export">导入/导出</a></li><li class="right"><a href="#save" id="save" class="active">保存</a></li>';
      topnav.innerHTML +=
        '<li class="right"><a href="javascript:void(addone())">添加一项</a></li>';
      var pre =
        '<table border="0" width="100%"><tr><td style="width:85%">名称</td><td align="center">单价(￥)</td><td align="center">操作</td></tr>';
      for (var i = 0; i < price.length; i++) {
        pre +=
          '<tr><td><input title="名称" oninput="price[' +
          i +
          '].name=this.value" style="width:99%" value="' +
          price[i].name +
          '"></td><td align="center"><input oninput="javascript:check(this,' +
          i +
          ')" style="width:99%" title="单价" step="0.01" type="number" value="' +
          price[i].price +
          '"></td><td><button onclick="javascript:del(' +
          i +
          ')">删除</button></tr>' +
          "</tr>";
      }
      pre += "</table>";
      content.innerHTML = pre;
    } else if (window.location.hash == "#export") {
      topnav.innerHTML =
        '<li><span>管理</span></li><li><a href="index.html">返回</a></li><li><a href="#">价格表</a></li><li class="right"></li>';
      content.innerHTML = '导出：<button onclick="javascript:exp()">点击导出</button><br>'+
                          '导入：<input type="file" oninput="javascript:input(this)"></button>';
    }
  } else {
    if (!window.localStorage) {
      alert("浏览器不支持保存设置，无法进行设置保存");
    } else {
      for (var i = 0; i < price.length; i++) {
        price[i] = JSON.stringify(price[i]);
      }
      localStorage.setItem("price", price.join("\uE333"));
      for (var i = 0; i < price.length; i++) {
        price[i] = JSON.parse(price[i]);
      }
      alert("保存成功！")
    }
    window.location.replace("#");
  }
}
function check(self, indx) {
  var num = self.value
  var s = num + "";
  if (!d) d = 0;
  if (s.indexOf(".") == -1) s += ".";
  s += new Array(d + 1).join("0");
  if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)) {
    var s = "0" + RegExp.$2,
      pm = RegExp.$1,
      a = RegExp.$3.length,
      b = true;
    if (a == d + 2) {
      a = s.match(/\d/g);
      if (parseInt(a[a.length - 1]) > 4) {
        for (var i = a.length - 2; i >= 0; i--) {
          a[i] = parseInt(a[i]) + 1;
          if (a[i] == 10) {
            a[i] = 0;
            b = i != 1;
          } else break;
        }
      }
      s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2");
    }
    if (b) s = s.substr(1);
    return (pm + s).replace(/\.$/, "");
  }
  price[indx].price =  num + "";
}
function addone() {
  price.push({ name: "", price: 0 });
  update();
}
function del(index) {
  for (i = 0, n = 0; i < price.length; i++) {
    if (price[i] != price[index]) {
      price[n++] = price[i];
    }
  }
  price.length -= 1;
  update();
  
}
function exp(){
  file = new File();
  
}
window.onload = update;
window.onhashchange = update;